﻿using System.Linq;
using System.Web.Mvc;
using System.Collections.Generic;

namespace Dinner.Models
{
	public class ItemRepository
	{
		private DinnerDataContext db = new DinnerDataContext();

		public IQueryable<Item> FindAllItems()
		{
			return db.Items;
		}

		public IEnumerable<SelectListItem> GetAllSelectItems()
		{
			var result = new List<SelectListItem>();
			foreach (var item in db.Items)
			{
				var listItem = new SelectListItem {Text = item.title, Value = item.item_id.ToString()};
				result.Add(listItem);
			}
			return result;
		}


		public Item GetItem(int id)
		{
			return db.Items.SingleOrDefault(m => m.item_id == id);
		}

		public void Add(Item item)
		{
			db.Items.InsertOnSubmit(item);
		}

		public void Delete(Item item)
		{
			db.Items.DeleteOnSubmit(item);
		}

		public void Save()
		{
			db.SubmitChanges();
		}
	}
}
